<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/5/21
 * Time: 10:09
 */
namespace Mengwu\Controller;

use Mengwu\Api\MwApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductImageApi;
use Shop\Api\ProductSkuApi;
use Shop\Model\ProductModel;

class SpdetailController extends BaseStoreController{
    
    public function index(){

        $id = I('get.id',-1);
        if($id == -1){
            $this->spError();
            exit();
        }

        //获取热门商品列表
        $hot = $this->hotsp($id);
        $this->assign('hotsp',$hot);

        //获取商品数据，调用API
        $data = [
            'pid' => $id,
            'uid' => $this->getUID(),
            'gid' => I('gid')
        ];
        if(!$this->isLogin()) unset($data['uid']);
        $result = mwApi(MwApi::BY_Product_detail, $data);
        
        if($result['status']){
            $info = $result['info'];

            //gallery_list
            $gallery_list = [];
            $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
            $i = 0;
            if(isset($info['main_img'])){
                $gallery_list[] = [
                    'id'=> ++$i,
                    's50'=>$PICTURE_REMOTE_URL.$info['main_img'].'&size=50',
                    's400'=>$PICTURE_REMOTE_URL.$info['main_img'].'&size=400',
                    'original'=>$PICTURE_REMOTE_URL.$info['main_img']
                ];
            }
            foreach($info['imgs'] as $val){
                $gallery_list[] = [
                    'id'=> ++$i,
                    's50'=>$PICTURE_REMOTE_URL.$val.'&size=50',
                    's400'=>$PICTURE_REMOTE_URL.$val.'&size=400',
                    'original'=>$PICTURE_REMOTE_URL.$val
                ];
            }
            $info['gallery_list'] = $gallery_list;

            $this->assignTitle($info['name']);

            $this->assign('spdetail', json_encode($info));
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display();
    }

    public function detail_info(){

        if(IS_POST){
            $id = I('post.id',-1);
            if($id==-1) $this->ajaxReturnErr('无此商品');
            $map = ['id' => $id];
            $result = apiCall(ProductApi::GET_INFO,array($map));
            if($result['status']){
                $this->ajaxReturnSuc(htmlspecialchars_decode($result['info']['detail']));
            }else{
                $this->ajaxReturnErr(null);
            }
        }
    }

    /**
     * 立即购买
     */
    public function buy(){
        
        if(IS_POST){
            $this->needLogin();
            //加入购物车
            $data = [
                'api_ver' => 101,
                'uid' => $this->getUID(),
                'pid' => I('pid'),
                'count' => I('count'),
                'psku_id' => I('psku_id'),
                'group_id' => I('group_id')
            ];

            $result = mwApi(MwApi::BY_ShoppingCart_add,$data);

            if(!$result['status']){
                $this->ajaxReturnErr($result['info']);
            }
            $cart_ids = $result['info']['cart_ids'];
            $cart_id = isset($cart_ids[0]) ? $cart_ids[0] : null;
            if(empty($cart_id)){
                $this->ajaxReturnErr('购买失败,错误代码001');
            }else{
                $this->ajaxReturnSuc($cart_id);
            }

        }
    }

    /**
     * 推荐商品
     */
    private function hotsp($p_id){

        $result = apiCall(ProductApi::GET_INFO,[['id'=>$p_id]]);
        $list = [];
        if($result['status']){
            if(!is_null($result['info'])){
                $cate_id = $result['info']['cate_id'];
                $map = [
                    'cate_id' => $cate_id,
                    'id' => ['neq',$p_id],
                    'status' => 1,
                    'onshelf' => 1,
                    'store_id' => 1
                ];
                $order = 'rand() limit 4';
                $result = apiCall(ProductApi::QUERY_NO_PAGING,[$map,$order]);
                if($result['status'] & !is_null($result['info'])){
                    $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                    $info = $result['info'];
                    foreach($info as &$val){
                        //取商品图
                        $result = apiCall(ProductImageApi::GET_INFO,[['pid'=>$val['id']]]);
                        if($result['status']){
                            $val['img'] = $PICTURE_REMOTE_URL.$result['info']['img_id'].'&size=200';
                        }
                        //取价格
                        $map = ['product_id'=>$val['id']];
                        $order = 'price asc';
                        $result = apiCall(ProductSkuApi::GET_INFO,[$map,$order]);
                        if($result['status']){
                            $val['price'] = $result['info']['price'];
                        }
                    }
                    $list = $info;
                }
            }
        }
        return $list;

    }

    public function hot(){
        if(IS_POST){
            $p_id = I('id');
            if(empty($p_id)) $this->ajaxReturnSuc([]);
            $list = $this->hotsp($p_id);
            $this->ajaxReturnSuc($list);
        }
    }


    /**
     * 商品ID错误页面
     */
    protected function spError(){
        $this->boye_display('Public:detail_error');
    }
    
}